package com.tsc.user.controller;

import com.tsc.common.response.ApiResponse;
import com.tsc.user.entity.UserInfo;
import com.tsc.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: chentl
 * @Date: 2025/08/15/17:31
 * @Description:
 */
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Value("${user.is_delete:false}")
    private boolean is_delete;

    /**
     * 获取nacos 配置信息
     * @return
     */
    @GetMapping("/info/isDelete")
    public boolean getIsDelete(){
        return is_delete;
    }

    @GetMapping("/info")
    public ApiResponse getUserInfo(@RequestParam String userId){
        return ApiResponse.success(userInfoService.selectUserInfo(userId));
    }

    @GetMapping("/info/name")
    public ApiResponse getUserInfoByName(@RequestParam String userName){
        return ApiResponse.success(userInfoService.selectUserInfoByName(userName));
    }

    @GetMapping("/info/list")
    public ApiResponse getUserInfoList(@RequestParam int status){
        return ApiResponse.success(userInfoService.selectUserInfoList(status));
    }

    @PostMapping("/info/add")
    public ApiResponse addUserInfo(@RequestBody UserInfo userInfo){
        return ApiResponse.success(userInfoService.insertUserInfo(userInfo));
    }

    @PostMapping("/info/update")
    public ApiResponse updateUserInfo(@RequestBody UserInfo userInfo){
        return ApiResponse.success(userInfoService.updateUserInfo(userInfo));
    }

    @DeleteMapping("/info/delete/{id}")
    public ApiResponse deleteUserInfo(@RequestParam Integer id) {
        return ApiResponse.success(userInfoService.deleteUserInfo(id));
    }

//    /**
//     * 测试sentinel 限流控制
//     * @return
//     */
//    @GetMapping("/info/test")
//    @SentinelResource(value = "test",blockHandler = "testFallback")
//    public ApiResponse test(){
//        return ApiResponse.success("测试成功");
//    }
//
//    public String testFallback(){
//        return "Too many requests, please try again later.";
//    }
//
//    /**
//     * 测试sentinel 降级控制
//     * @return
//     */
//    @GetMapping("/info/test2")
//    @SentinelResource(value = "test2",fallback = "test2Fallback")
//    public ApiResponse test2(){
//        return ApiResponse.success("测试成功");
//    }
//
//    public String test2Fallback(){
//        return "Service is unavailable, please try again later.";
//    }


}
